import { observable, action, toJS } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Tools } from 'weaver-mobile';
import { SearchAdvanced, RouteLayout } from 'weaver-mobile-page';
const getLabel = Tools.getLabel;

export default class HomePageStore {

    @observable comObj = {};
    @observable search = {};
    @observable enters = [];//入口信息
    @observable createdateselectParams = { createdateselect: "2" };//默认条件本周
    @observable createrselectParams = { creatertype: "4" };//默认条件本部门
    @observable createDateSelectOptions = [];//创建日期下拉选项
    @observable createrSelectOptions = [];//创建人员下拉选项
    @observable sellChanceDateStr = "";//销售简报创建日期文本描述（store里用）
    @observable sellChancePersonStr = "";//销售简报人员范围文本描述（store里用）
    @observable sellChanceSearchStr = "";//销售简报筛选文本描述（compnent里用）
    @observable sellchanceReports = [];//销售简报信息
    @observable sellReportLayout = {};
    @observable remindLayout = {};//遗忘提醒列表路由层
    @observable portalLayout = {};//快捷入口路由层
    @observable contactPlanInfo = {count:0,title:"",startTime:"",endTime:""};
    @observable sellsReportLoading = false;
    @observable contactPlanLoading = false;
    @observable remindLoading = false;
    @observable remindInfo = {total:0,customerId:"",customerName:"",lastestContactDate:"",uncontactDate:""};
    @observable workplanCreateLayout = {};
    @observable workplanViewLayout = {};
    @observable customerCardLayout = {};
    @observable contactHomeLayout = {};
    @observable crmListLayout = {};
    @observable enterLayout = {};
    @observable refreshing = false;

    @action
    doNew = (comObj) => {
        this.comObj = comObj;
        const route = comObj.props.match.path;
        this.search = new SearchAdvanced.Store({ route: route, id: 'search' })
        this.sellReportLayout = new RouteLayout.Store({ route: route, id: 'sellReportLayout'});
        this.portalLayout = new RouteLayout.Store({ route: route, id: 'portalLayout'});
        this.workplanCreateLayout = new RouteLayout.Store({ route: route, id: 'workplanCreateLayout' });
        this.workplanViewLayout = new RouteLayout.Store({ route: route, id: 'workplanViewLayout' });   
        this.remindLayout = new RouteLayout.Store({ route: route, id: 'remindLayout'});
        this.customerCardLayout = new RouteLayout.Store({ route: route, id: 'customerCardLayout'});
        this.contactHomeLayout = new RouteLayout.Store({ route: route, id: 'contactHomeLayout'});
        this.enterLayout = new RouteLayout.Store({ route: route, id: 'enterLayout'});

        this.enters = [{
            icon: '/images/ecology9/crm/mobile-home-customer.png',
            text: getLabel(2113,'客户管理'),  
            comPath: "../list/CrmList",          
            comParams : {type:"crm_list",listTabIndex:"my"}
        }, {
            icon: '/images/ecology9/crm/mobile-home-sellchance.png',
            text: getLabel(82545,'商机管理'),
            comPath: "../list/CrmList",          
            comParams : {type:"crm_chance",listTabIndex:"my"}
        }, {
            icon: '/images/ecology9/crm/mobile-home-contacter.png',
            text: getLabel(22398,'联系人管理'),
            comPath: "../list/CrmList",          
            comParams : {type:"crm_contacter",listTabIndex:"my"}
        }, {
            icon: '/images/ecology9/crm/mobile-home-contacts.png',
            text: getLabel(6082,'客户联系'),
            comPath: "../list/ContactHomePage",
            comParams : {subType:"contact"}
        }];
    }

    @action
    doInit = () => {
        // this.refreshing = true;
        this.getHpSellsCondition();//获取销售简报高级搜索条件
        this.getContactPlan();//获取拜访计划信息
        this.getRemindInfo();//获取遗忘客户提醒信息
    }

    //获取销售简报的高级搜索表单
    @action
    getHpSellsCondition = () => {
        this.search.getData({
            url: '/api/crm/mobile/getHpSellsCondition',
            method: 'POST',
            params: toJS({ ...this.createdateselectParams, ...this.createrselectParams })
        }).then((data) => {
            this.createDateSelectOptions = data.conditioninfo[0].items[0].options;
            this.createrSelectOptions = data.conditioninfo[0].items[1].options;
            this.getHpSellsInfo();//获取销售简报信息
        });
    }

    //获取销售简报统计信息
    @action
    getHpSellsInfo = () => {
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams });
        // if (sellChanceSearchValue.createdateselect === '6') {
        //     if (!sellChanceSearchValue.createdatefrom && !sellChanceSearchValue.createdateto) {
        //         return;
        //     }
        // }
        // if (sellChanceSearchValue.creatertype === '1' || sellChanceSearchValue.creatertype === '2' || sellChanceSearchValue.creatertype === '3') {
        //     let createrids = [];
        //     if (sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype] && sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype].length > 0) {
        //         sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype].forEach((_el) => {
        //             createrids.push(_el.id);
        //         });
        //         sellChanceSearchValue.createrids = `${createrids}`;
        //     } else {
        //         return;
        //     }
        // }
        this.getSellChanceSearchStr();
        this.sellsReportLoading = true;
        delete sellChanceSearchValue['createrid' + sellChanceSearchValue.creatertype];
        Apis.getHpSellsInfo(toJS(sellChanceSearchValue)).then((result) => {
            this.sellchanceReports = result.data;
            this.sellsReportLoading = false;
        });
    }

    //获取搜索表单数据（表单数据变更时触发）
    @action
    getSellChanceSearchValue = (params) => {
        // if (params.createdateselect) {
        //     this.createdateselectParams = params;
        // } else if (params.creatertype) {
        //     this.createrselectParams = params;
        // }
        if (params.createdateselect) {
            if (params.createdateselect === '6') {
                if (!params.createdatefrom && !params.createdateto) {
                    return;
                }
            }
            this.createdateselectParams = params;
        } else if (params.creatertype) {
            if (params.creatertype === '1' || params.creatertype === '2' || params.creatertype === '3') {
                let createrids = [];
                if (params['createrid' + params.creatertype] && params['createrid' + params.creatertype].length > 0) {
                    params['createrid' + params.creatertype].forEach((_el) => {
                        createrids.push(_el.id);
                    });
                    params.createrids = `${createrids}`;
                } else {
                    return;
                }
            }
            this.createrselectParams = params;
        }
    }

    //获取创建日期的文本描述
    @action
    getSellChanceSearchStr = () => {
        const sellChanceSearchValue = toJS({ ...this.createdateselectParams, ...this.createrselectParams });

        const createdateselect = sellChanceSearchValue.createdateselect;
        this.createDateSelectOptions.forEach((option) => {
            if (createdateselect === option.key) {
                this.sellChanceDateStr = option.showname;
            }
        });

        let sellChancePerson = [];
        const creatertype = sellChanceSearchValue.creatertype;
        if (creatertype === '1' || creatertype === '2' || creatertype === '3') {
            const createOption = sellChanceSearchValue['createrid' + creatertype];
            if (createOption) {
                createOption.forEach((option) => {
                    sellChancePerson.push(option.name);
                });
            }
            if (sellChancePerson && sellChancePerson.length > 0) {
                this.sellChancePersonStr = ' | ' + `${sellChancePerson}`;
            }
        } else {
            this.createrSelectOptions.forEach((option) => {
                if (creatertype === option.key) {
                    this.sellChancePersonStr = ' | ' + option.showname;
                }
            });
        }

        this.sellChanceSearchStr = this.sellChanceDateStr + this.sellChancePersonStr;
    }

    //销售简报搜索条件重置方法
    @action
    getHpSellsReset = () => {
        this.createdateselectParams = { createdateselect: "2" };//默认条件本周
        this.createrselectParams = { creatertype: "4" };//默认条件本部门
    }

    @action
    doClear = () => {
        this.comObj={};
    }

    @action
    getContactPlan = () => {
        this.contactPlanLoading = true;
        this.contactPlanInfo.count = 0;
        Apis.getContactPlanInfo({}).then((data) => {            
            this.contactPlanLoading = false;
            if(data.count > 0){
                this.contactPlanInfo = data;
            }
        });        
    }

    @action
    getRemindInfo = () => {
        this.remindLoading = true;
        this.remindInfo.total = 0;
        Apis.getRemindInfo({showFirst:true}).then((data) => {
            this.remindLoading = false;
            if(data.datas.total > 0){
                this.remindInfo = data.datas;
            }
        });        
    }
}